package view;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

public class ConverterPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private TitledBorder title;
	private JTextField textField;
	private JComboBox comboBox;

	public ConverterPanel(String label, String textField, Object[][] items) {

		super();
		this.title = BorderFactory.createTitledBorder(BorderFactory
				.createLineBorder(Color.black), label);
		this.title.setTitleJustification(TitledBorder.LEFT);
		this.setBorder(title);
		this.textField = new JTextField(textField, 20);
		this.textField.setDocument(new DigitLimited(this.textField));
		this.textField.setText("0.0");
		this.comboBox = new JComboBox(items);
		this.comboBox.setRenderer(new ExchangerComboBoxRenderer());
		this.setLayout(new GridLayout(3, 1));
		this.add(this.textField);
		this.add(this.comboBox);
	}

	public JTextField getTextField() {
		return this.textField;
	}
	
	public void setTitle(String title){
		this.title.setTitle(title);
		repaint();
	}

	public JComboBox getComboBox() {
		return this.comboBox;
	}

	public void setEditable() {
		this.textField.setEditable(false);
	}

	public String obtainTextFieldString() {
		return this.textField.getText();
	}

	public String obtainComboBoxSelectedItem() {
		return ((String[])this.comboBox.getSelectedItem())[0];
	}

	public void changeTextField(String text) {
		this.textField.setText(text);
	}

	public void changeComboBoxList(Object[][] items) {
		this.comboBox.setModel(new DefaultComboBoxModel(items));
	}

	public void selectItem(String item) {
		int index = -1;
		seek: for (int i = 0; i < this.comboBox.getItemCount(); i++) {
			if (((String[])this.comboBox.getItemAt(i))[0].equals(item)) {
				index = i;
				break seek;
			}
		}
		if (index != -1) {
			this.comboBox.setSelectedIndex(index);
		}
	}

}
